<?php

function Get_Tree_From_Arr($datas)
{
    foreach ($datas as $dk => $dv) {
        $datas[$dv['pid']]['children'][$dv['id']] = &$datas[$dv['id']];
    }
    return isset($datas[0]['children']) ? $datas[0]['children'] : array();
}
$tree = Get_Tree_From_Arr($items);
// var_dump($tree);
echo json_encode($tree);